package com.hankol.message;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/7/20 12:53
 */
public class PopupUtil {


    public static PopupWindow showPopwindow(final Activity activity, View contentView, int width, int height) {
        PopupWindow popupWindow = new PopupWindow(contentView, width, height);
        popupWindow.setFocusable(true);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(true);
        //设置可以点击
        popupWindow.setTouchable(true);
        setBackgroundAlpha(activity, 0.5f);//设置屏幕透明度
        //进入退出的动画，指定刚才定义的style
        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(activity, 1.0f);
            }
        });
        return popupWindow;
    }


    public static PopupWindow showPartyApplyPop(final Activity activity, View contentView, int width, int height) {
        PopupWindow popupWindow = new PopupWindow(contentView, width, height);
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(false);
        //设置可以点击
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);// 取得焦点
        // 设置这两个属性
        setBackgroundAlpha(activity, 0.5f);//设置屏幕透明度
        //进入退出的动画，指定刚才定义的style
        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.setOnDismissListener(() -> setBackgroundAlpha(activity, 1.0f));
        return popupWindow;
    }


    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }


    /**
     * 聊天界面消息长按
     *
     * @param activity
     */
    public static void showImMesLongClick(Activity activity, boolean isSelf, View.OnClickListener listener) {
        View mImMesLongClick = LayoutInflater.from(activity).inflate(R.layout.popup_im_longclick, null);
        TextView mTvImDeleteMes = mImMesLongClick.findViewById(R.id.tv_im_deleteMes);
        TextView mTvImReportMes = mImMesLongClick.findViewById(R.id.tv_im_reportMes);
//        TextView mTvImCopyMes = mImMesLongClick.findViewById(R.id.tv_im_copyMes);
        View delBottomView = mImMesLongClick.findViewById(R.id.v_del_bottom);

        PopupWindow popupWindow = PopupUtil.showPartyApplyPop(activity, mImMesLongClick, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);// 取得焦点
        setBackgroundAlpha(activity, 0.5f);//设置屏幕透明度
        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.setOnDismissListener(() -> setBackgroundAlpha(activity, 1.0f));
        popupWindow.showAtLocation(mImMesLongClick, Gravity.CENTER, 0, 0);

        mTvImDeleteMes.setText("删除消息");
//        mTvImCopyMes.setText("复制消息");
//        mTvImReportMes.setVisibility(isSelf ? View.GONE : View.VISIBLE);
//        delBottomView.setVisibility(isSelf ? View.GONE : View.VISIBLE);
        mTvImDeleteMes.setOnClickListener(view -> {
            if (listener != null)
                listener.onClick(view);
            popupWindow.dismiss();
        });
        mTvImReportMes.setOnClickListener(view -> {
            if (listener != null)
                listener.onClick(view);
            popupWindow.dismiss();
        });
//        mTvImCopyMes.setOnClickListener(view -> {
//            if (listener != null)
//                listener.onClick(view);
//            popupWindow.dismiss();
//        });
    }


    public interface OnViewClickListener {
        void onClick(View view);
    }

}
